﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Percolation
{
    public struct NetCellColor
    {
        public const int Black = 0;
        public const int White = 1;
    }

    [Serializable]
    public class NetStatistics
    {
        public double AvgBlackCellRow { get; private set; }
        public double AvgBlackCellColumn { get; private set; }
        public int NullRowCount { get; private set; }
        public int NullColumnCount { get; private set; }
        public int BlackCellCount { get; private set; }

        public NetStatistics(NetInterface net)
        {
            int [] BlackCellRowArray = new int [net.getHeight()];
            int [] BlackCellColumnArray = new int [net.getWidth()];

            Array.ForEach(BlackCellRowArray, x => x = 0);
            Array.ForEach(BlackCellColumnArray, x => x = 0);
            BlackCellCount = 0;
            NullRowCount = 0;
            NullColumnCount = 0;

            for (int j = 0; j < net.getHeight(); ++j)
            {
                for (int i = 0; i < net.getWidth(); ++i)
                {
                    if (net.getValue(i, j) == NetCellColor.Black)
                    {
                        ++BlackCellCount;
                        ++BlackCellRowArray[j];
                        ++BlackCellColumnArray[i];
                    }
                }
            }

            Array.ForEach(BlackCellRowArray,
                x =>
                {
                    AvgBlackCellRow += x;
                    if (x == 0)
                        ++NullRowCount;
                }
            );
            AvgBlackCellRow /= net.getHeight();

            Array.ForEach(BlackCellColumnArray,
                x =>
                {
                    AvgBlackCellColumn += x;
                    if (x == 0)
                        ++NullColumnCount;
                }
            );
            AvgBlackCellColumn /= net.getWidth();
        }
    }

    public interface NetInterface
    {
        int getWidth();
        int getHeight();

        int getValue(int x, int y);
    }

    public sealed class NetProcessor
    {
        public static NetStatistics getStatistics(NetInterface net)
        {
            return new NetStatistics(net);
        }

    }
}
